﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Text;
using System.Collections;

namespace Catering.Catering_Page.Caipin.Caipin_jcsz
{
    public partial class Leibieshezhi : System.Web.UI.Page
    {

        Catering_Rules.T_RawMaterials crrm = new Catering_Rules.T_RawMaterials();
        Catering_Rules.DishType crdt = new Catering_Rules.DishType();
        public DataTable dt_caipinType;

        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(Leibieshezhi));
            if (!IsPostBack)
            {
                this.LoadRep();
            }
        }

        //绑定列表数据
        private void LoadRep()
        {
            this.dt_caipinType = new Catering_Rules.DishType().GetAllList(false).Tables[0];

            this.Rep_RawMaterials.DataSource = new Catering_Rules.T_RawMaterials().GetAllList().Tables[0];
            this.Rep_RawMaterials.DataBind();
        }

        //获取原料大类信息
        [Ajax.AjaxMethod]
        public DataTable GetTypeby(string strid)
        {
            return new Catering_Rules.T_RawMaterials().GetList("id="+strid).Tables[0];
        }

        //获取原料细类信息
        [WebMethod]
        public static ArrayList Select_Max_Type(string strMaxtype)
        {
            ArrayList list = new ArrayList();
            StringBuilder sbb = new StringBuilder();
            DataTable dt = new Catering_Rules.RawMaterialsInfo().GetList("RMID=" + strMaxtype).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                int i = 1;
                foreach (DataRow row in dt.Rows)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("<tr id=\"Raw_tr_" + row["id"].ToString() + "\" uid=\"" + row["id"].ToString() + "\">");
                    sb.Append("<td class='xh_td'>" + i.ToString() + "</td>");
                    sb.Append("<td cid=\"name\">" + row["Name"].ToString() + "</td>");
                    sb.Append("<td cid=\"back\">" + (row["Back"].ToString() == "" ? "&nbsp" : row["Back"].ToString()) + "</td>");
                    sb.Append("<td align='center'>");
                    sb.Append("<img src='../../../Images/file.gif' style=\"cursor: pointer;\" onclick=\"EditRawTypeInfoByID('" + row["id"].ToString() + "')\" />&nbsp;&nbsp;&nbsp;&nbsp;");
                    sb.Append("<a href='javascript:void(0);' onclick='DelRawMaterialsInfoByID(" + row["id"].ToString() + ")'><img src='../../../Images/trash.gif'/></a>");
                    sb.Append("</td>");
                    sb.Append("</tr>");
                    sbb.Append(sb.ToString());
                    i++;
                }
                list.Add(sbb.ToString());
            }
            return list;
        }

        //修改原料大类
        [WebMethod]
        public static string Edit_RawMaterials(string CID, string TypeName, string Back)
        {
            Catering_Model.E_RawMaterials md = new Catering_Model.E_RawMaterials();
            md.id = Convert.ToInt32(CID);
            md.TypeName = TypeName;
            md.Back = Back;
            return new Catering_Rules.T_RawMaterials().Update(md)?"OK":"NO";
        }

        //添加原料大类
        [WebMethod]
        public static string Add_RawMaterials(string TypeName, string Back)
        {
            Catering_Model.E_RawMaterials md = new Catering_Model.E_RawMaterials();
            md.TypeName = TypeName;
            md.Back = Back;
            new Catering_Rules.T_RawMaterials().Add(md);
            return "OK";
        }

        //删除原料大类
        [WebMethod]
        public static string Raw_Del(string Raw_Del_Id)
        {
            bool IsExists = false;
            DataTable dt_typeinfo = new Catering_Rules.RawMaterialsInfo().GetList("RMID=" + Raw_Del_Id).Tables[0];
            if (dt_typeinfo != null && dt_typeinfo.Rows.Count > 0)
            {
                foreach (DataRow row in dt_typeinfo.Rows)
                {
                    DataTable dt_RawMaterial = new Catering_Rules.T_RawMaterial().GetList("RAWIDS=" + row["id"].ToString()).Tables[0];
                    if (dt_RawMaterial != null && dt_RawMaterial.Rows.Count > 0)
                    {
                        IsExists = true;

                    }
                }
            }
            if (IsExists)
            {
                return "IsCunZai";
            }
            else
            {
                new Catering_Rules.RawMaterialsInfo().DeleteList(" RMID=" + Raw_Del_Id);
                return new Catering_Rules.T_RawMaterials().Delete(Convert.ToInt32(Raw_Del_Id)) ? "OK" : "NO";
            }
        }

        //删除原料细类
        [WebMethod]
        public static string DelRawMaterialsInfoByID(string Raw_Del_Id)
        {
            DataTable dt_RawMaterial = new Catering_Rules.T_RawMaterial().GetList("RAWIDS=" + Raw_Del_Id).Tables[0];
            if (dt_RawMaterial != null && dt_RawMaterial.Rows.Count > 0)
            {
                return "IsCunZai";
            }
            else
            {
                return new Catering_Rules.RawMaterialsInfo().Delete(Convert.ToInt32(Raw_Del_Id)) ? "OK" : "NO";
            }
        }

        //获取菜类信息
        [Ajax.AjaxMethod]
        public DataTable GetDishType(string strid)
        {
            return new Catering_Rules.DishType().GetList("id=" + strid).Tables[0];
        }

        //修改菜类
        [WebMethod]
        public static string Edit_DishType(string CID, string DTName, string Back, string StrParentID)
        {
            int parentid = 0;
            Int32.TryParse(StrParentID, out parentid);
            Catering_Model.DishType md = new Catering_Model.DishType();
            md.id =Convert.ToInt32(CID);
            md.DT = DTName;
            md.Back = Back;
            md.ParentID = parentid;
            return new Catering_Rules.DishType().Update(md) ? "OK" : "NO";
        }

        //添加菜类
        [WebMethod]
        public static string Add_DishType(string DTName, string Back,string StrParentID)
        {
            int parentid = 0;
            Int32.TryParse(StrParentID, out parentid);
            Catering_Model.DishType md = new Catering_Model.DishType();
            md.id = new Catering_Rules.DishType().GetMaxId();
            md.DT = DTName;
            md.Back = Back;
            md.ParentID = parentid;
            if (parentid == 0)
            {
                md.TypeLevel = 0;
            }
            else //设置菜品类别等级
            {
                Catering_Rules.DishType dal = new Catering_Rules.DishType();
                Catering_Model.DishType typeinfo=dal.GetModel(parentid);
                md.TypeLevel = typeinfo.TypeLevel + 1;
            }
            new Catering_Rules.DishType().Add(md);
            return "OK";
        }

        //删除菜类
        [WebMethod]
        public static string DelDishTypesByid(string id)
        {
            bool IsExists = false;
            DataTable dt_DishTypes = new Catering_Rules.DishTypes().GetList("DTID=" + id).Tables[0];
            if (dt_DishTypes != null && dt_DishTypes.Rows.Count > 0)
            {
                foreach (DataRow row in dt_DishTypes.Rows)
                {
                    DataTable dt_Dish = new Catering_Rules.T_Dish().GetList("DTID=" + row["id"].ToString()).Tables[0];
                    if (dt_Dish != null && dt_Dish.Rows.Count > 0)
                    {
                        IsExists = true;
                    }
                }
            }

            if (IsExists)
            {
                return "IsCunZai";
            }
            else
            {
                new Catering_Rules.DishTypes().DeleteListBywhere("DTID=" + id);
                return new Catering_Rules.DishType().Delete(Convert.ToInt32(id)) ? "OK" : "NO";
            }
        }

        //获取菜细类列表
        [WebMethod]
        public static ArrayList GetDishTypeInfo(string DishTypeID)
        {
            ArrayList al = new ArrayList();
            DataTable dt_DishTypes = new Catering_Rules.DishTypes().GetList("DTID="+DishTypeID).Tables[0];
            StringBuilder sb = new StringBuilder();
            int i = 1;
            if (dt_DishTypes != null && dt_DishTypes.Rows.Count > 0)
            {
                foreach (DataRow row in dt_DishTypes.Rows)
                {
                    sb.Append("<tr id=\"Dish_tr_" + row["id"].ToString() + "\" uid=\"" + row["id"].ToString() + "\">");
                    sb.Append("<td class='xh_td'>" + i.ToString() + "</td>");
                    sb.Append("<td cid=\"name\">" + row["DTName"].ToString() + "</td>");
                    sb.Append("<td cid=\"back\">" + (row["Back"].ToString() == "" ? "&nbsp" : row["Back"].ToString()) + "</td>");
                    sb.Append("<td align='center'>");
                    sb.Append("<img src='../../../Images/file.gif' style=\"cursor: pointer;\" onclick=\"EditDishTypeInfoByID('" + row["id"].ToString() + "')\" />&nbsp;&nbsp;&nbsp;&nbsp;");
                    sb.Append("<a href='javascript:void(0);' onclick='DelDishTypeInfoByID(" + row["id"].ToString() + ")'><img src='../../../Images/trash.gif'/></a>");
                    sb.Append("</td>");
                    sb.Append("</tr>");
                    i++;
                }
                dt_DishTypes.Dispose();
            }
            al.Add(sb.ToString());
            al.Add("OK");
            return al;
        }

        //删除菜细类
        [WebMethod]
        public static string DelDishTypeInfo(string DishTypeInfoId)
        {
            DataTable dt_Dish = new Catering_Rules.T_Dish().GetList("DTID=" + DishTypeInfoId).Tables[0];
            if (dt_Dish != null && dt_Dish.Rows.Count > 0)
            {
                return "IsCunZai";
            }
            else
            {
                return new Catering_Rules.DishTypes().Delete(Convert.ToInt32(DishTypeInfoId)) ? "OK" : "NO";
            }
        }

        //添加菜细类
        [WebMethod]
        public static string SaveDishTypeInfo(string DishTypeID, string DishTypeInfo, string DishTypeInfoEdit)
        {
            if (DishTypeInfo != "")
            {
                string[] s_DishInfo = DishTypeInfo.TrimEnd(',').Split(',');
                foreach (string str in s_DishInfo)
                {
                    Catering_Model.DishTypes md = new Catering_Model.DishTypes();
                    md.DTID = Convert.ToInt32(DishTypeID);
                    md.DTName = str.Split('|')[0];
                    md.Back = str.Split('|')[1];
                    new Catering_Rules.DishTypes().Add(md);
                }
            }

            if (DishTypeInfoEdit != "")
            {
                string[] s_DishInfoEdit = DishTypeInfoEdit.TrimEnd(',').Split(',');
                foreach (string str in s_DishInfoEdit)
                {
                    Catering_Model.DishTypes md = new Catering_Model.DishTypes();
                    md.id =Convert.ToInt32(str.Split('|')[0]);
                    md.DTID = Convert.ToInt32(DishTypeID);
                    md.DTName = str.Split('|')[1];
                    md.Back = str.Split('|')[2];
                    new Catering_Rules.DishTypes().Update(md);
                }
                
            }
            return "OK";
        }

        public void butRawS_Click(string str)
        {
            string[] TypeName = str.Split(new char[] { ',' });
            Catering_Model.E_RawMaterials[] modes = new Catering_Model.E_RawMaterials[TypeName.Length];

            for (int i = 0; i < TypeName.Length; i++)
            {
                if (TypeName[i] != "" && TypeName[i] != null)
                {
                    Catering_Model.E_RawMaterials cmrm = new Catering_Model.E_RawMaterials();
                    cmrm.id = crrm.GetMaxId();
                    cmrm.TypeName = TypeName[i];
                    cmrm.Back = "";
                    cmrm.temp1 = "";
                    cmrm.temp2 = "";
                    modes[i] = cmrm;
                }
            }
            crrm.Add(modes);
        }

        public void butDishS_Click(string str)
        {

            string[] TypeName = str.Split(new char[] { ',' });
            Catering_Model.DishType[] modes = new Catering_Model.DishType[TypeName.Length];

            for (int i = 0; i < TypeName.Length; i++)
            {
                if (TypeName[i] != "" && TypeName[i] != null)
                {
                    Catering_Model.DishType cmrm = new Catering_Model.DishType();
                    cmrm.id = crdt.GetMaxId();
                    cmrm.DT = TypeName[i];
                    cmrm.Back = "";
                    cmrm.temp1 = "";
                    cmrm.temp2 = "";
                    modes[i] = cmrm;
                }
            }
            crdt.Add(modes);
        }


        //保存原料细类
        [WebMethod]
        public static string butRaw_New_Min_Click(string str, string Type_Max_ID, string RawTypeInfoEdit)
        {
            if (str != "")
            {
                string[] TypeName = str.TrimEnd('|').Split('|');
                for (int i = 0; i < TypeName.Length; i++)
                {
                    string[] info = TypeName[i].Split(',');
                    Catering_Model.RawMaterialsInfo rm = new Catering_Model.RawMaterialsInfo();
                    rm.id = new Catering_Rules.RawMaterialsInfo().GetMaxId();
                    rm.Name = info[0];
                    rm.RMID = Convert.ToInt32(Type_Max_ID);
                    rm.Back = info[1];
                    rm.temp1 = "";
                    rm.temp2 = "";
                    new Catering_Rules.RawMaterialsInfo().Add(rm);
                }
            }

            if (RawTypeInfoEdit != "")
            {
                string[] TypeName = RawTypeInfoEdit.TrimEnd('|').Split('|');
                for (int i = 0; i < TypeName.Length; i++)
                {
                    string[] info = TypeName[i].Split(',');
                    Catering_Model.RawMaterialsInfo rm = new Catering_Model.RawMaterialsInfo();
                    rm.id = Convert.ToInt32(info[0]);
                    rm.Name = info[1];
                    rm.RMID = Convert.ToInt32(Type_Max_ID);
                    rm.Back = info[2];
                    rm.temp1 = "";
                    rm.temp2 = "";
                    new Catering_Rules.RawMaterialsInfo().Update(rm);
                }
            }
            
            return "OK";
        }

        public string GetlibraryName(string strid)
        {
            DataTable dt = new Catering_Rules.library().GetList("id="+strid).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0]["LName"].ToString();
            }
            else
            { 
                return "&nbsp;";
            }
            
        }

        public string Getlibrary_option()
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = new Catering_Rules.library().GetAllList().Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append("<option value=\\\"" + dt.Rows[i]["id"].ToString() + "\\\">" + dt.Rows[i]["LName"].ToString() + "</option>");
                }
            }
            return sb.ToString();
        }


        //获取菜品类别Html
        public string GetDistTypeHtml()
        {
            //一级模板
            string LoopTemplate = @"<ul id='row1'>
                                        <li class='WidthID'>{$id}</li>
                                        <li class='WidthName'>{$imglist}<a href='javascript:void(0);' onclick='rowView({$id})'><img src='{$IcoImg}' id='Icon{$id}'/>{$DT}</a></li>
                                        <li class='WidthOperate'>
                                            <a href='javascript:void(0);' onclick='Dish_Add({$id})'>添加子类</a> | 
                                            <a href='javascript:void(0);' onclick='Dish_Type_Edit({$id})'>修改</a> | 
                                            <a href='javascript:void(0);' onclick='Clear_Dish_Type({$id})'>删除</a>
                                        </li>
                                    </ul>";
            //子模板
            string SubTemplate="<div class='displaynone' id='SubItem{0}'>{1}</div>";

            StringBuilder TypeHtml = new StringBuilder();
            DataRow[] RowList = dt_caipinType.Select("ParentID=0");
            string SubTypeHtml = AddTypeHtml(RowList, LoopTemplate, SubTemplate);
            TypeHtml.Append(SubTypeHtml);
            return TypeHtml.ToString();
        }

        /// <summary>
        /// 添加子类HTML
        /// </summary>
        /// <param name="RowList"></param>
        /// <param name="Template"></param>
        /// <returns></returns>
        public string AddTypeHtml(DataRow[] RowList, string Template, string SubTemplate)
        {
            StringBuilder TypeHtml = new StringBuilder();
            for (int i = 0; i < RowList.Length; i++)
            {
                DataRow row = RowList[i];
                bool IsExistsSubItem = (dt_caipinType.Select("ParentID=" + row["id"]).Count() > 0);
                bool IsEnd = (i == RowList.Length - 1);
                TypeHtml.Append(this.AddTypeHtml(row, Template, IsExistsSubItem, IsEnd));
                if (IsExistsSubItem)
                {
                    string SubTypeHtml = AddTypeHtml(dt_caipinType.Select("ParentID=" + row["id"]), Template, SubTemplate);
                    TypeHtml.AppendFormat(SubTemplate, row["id"].ToString(), SubTypeHtml);
                }
            }
            return TypeHtml.ToString();
        }


        /// <summary>
        /// 添加子类HTML
        /// </summary>
        /// <param name="TypeRow"></param>
        /// <param name="Template"></param>
        /// <returns></returns>
        public string AddTypeHtml(DataRow TypeRow, string Template,bool IsExistsSubItem,bool IsEnd)
        {
            StringBuilder SubTypeHtml = new StringBuilder();
            Template = Template.Replace("{$id}", TypeRow["id"].ToString());
            Template = Template.Replace("{$DT}", TypeRow["DT"].ToString());
            Template = Template.Replace("{$IcoImg}", IsExistsSubItem ? "/Images/Tree/folder4.gif" : "/Images/Tree/folder3.gif");
            StringBuilder strimg = new StringBuilder();
            int level = Convert.ToInt32(TypeRow["TypeLevel"].ToString());
            if (level > 0)
            {
                for (int i = 0; i < level-1; i++)
                {
                    strimg.Append("<img src='/Images/Tree/line3.gif' />");
                }
                strimg.Append("<img src='/Images/Tree/line" + (IsEnd ? "2" : "1") + ".gif' />");
            }

            Template = Template.Replace("{$imglist}", strimg.ToString());
            SubTypeHtml.Append(Template);
            return SubTypeHtml.ToString();
        }


        

    }
}